package com.yjy.fuyao.message.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjy.fuyao.message.entity.Event;
import com.yjy.fuyao.message.entity.Message;
import com.yjy.fuyao.message.event.EventProducer;
import com.yjy.fuyao.message.service.MessageService;
import com.yjy.fuyao.utils.result.R;
import com.yjy.fuyao.utils.util.JwtUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/admin/message")
public class AdminMessageController {

    @Resource
    private MessageService messageService;

    @Resource
    private EventProducer producer;

    @GetMapping(value = "/msgList/{page}/{limit}")
    public R msgList(@ApiParam(name = "page", value = "当前页码", required = true)
                          @PathVariable Long page,

                          @ApiParam(name = "limit", value = "每页记录数", required = true)
                          @PathVariable Long limit){
        Page<Message> messagePage = new Page<>(page, limit);
        Map<String,Object> map = messageService.pageList(messagePage);
        return R.ok().data(map);
    }

    @PostMapping(value = "/sendSysMsg")
    public R sendSysMsg(@RequestBody Event event){
        System.out.println("开始发送消息...");
        producer.sendMessage(event);
        return R.ok();
    }

    @DeleteMapping("/remove")
    public R removeById(
            @ApiParam(name = "id", value = "消息id", required = true)
            @RequestParam("id") String id
    ){
        boolean result = messageService.removeMessageById(id);
        return R.ok();
    }
}
